package com.zspringboot.demo.controller;




import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.zspringboot.demo.common.Result;

import java.util.List;
import com.zspringboot.demo.service.IResultHomeService;
import com.zspringboot.demo.entity.ResultHome;



import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author laolang
 * @since 2024-03-12
 */
@RestController
@RequestMapping("/result-home")
public class ResultHomeController {


    @Resource
    private IResultHomeService resultHomeService;

    // 新增/修改
    @PostMapping
    public Result save(@RequestBody ResultHome resultHome) {
            return Result.success(resultHomeService.saveOrUpdate(resultHome));
            }

    // 删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
            return Result.success(resultHomeService.removeById(id));
            }
    // 批量删除
    @PostMapping("/del/batch")
    public Result deleteBacth(@RequestBody List<Integer> ids){
        return Result.success(resultHomeService.removeByIds(ids));
        }
    // 查询所有数据
    @GetMapping
    public Result findAll() {
            return Result.success(resultHomeService.list());
            }

    // 根据id 查询
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
            return Result.success(resultHomeService.getById(id));
            }

    // 分页
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize,
                            @RequestParam(defaultValue = "") String username,
                            @RequestParam(defaultValue = "") String nickname) {
            QueryWrapper<ResultHome> queryWrapper = new QueryWrapper<>();

            if (!"".equals(username))
            queryWrapper.like("username",username);
            if (!"".equals(nickname))
            queryWrapper.like("nickname",nickname);
            return Result.success(resultHomeService.page(new Page<>(pageNum, pageSize),queryWrapper));
            }

}

